Public Class frmCoach


    Private Sub frmCoach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadCoaches()
        loadLeagues()
    End Sub
    Private Sub loadCoaches()
        cboExistingCoaches.Items.Clear()

        Dim myCoachList As coachingAppObjects.ro_coachRootList
        Dim item As coachingAppObjects.ro_coachChild

        myCoachList = coachingAppObjects.ro_coachRootList.Getro_coachRootList
        cboExistingCoaches.Items.Add("Please Select")
        For Each item In myCoachList
            cboExistingCoaches.Items.Add(item)
        Next
        cboExistingCoaches.SelectedIndex = 0
    End Sub
    Private Sub loadLeagues()
        'Get LeagueList
        cboLeagues.Items.Clear()

        Dim myLeagueList As coachingAppObjects.ro_leagueRootList
        Dim item As coachingAppObjects.ro_leagueChild

        myLeagueList = coachingAppObjects.ro_leagueRootList.GetLeagueRootList

        cboLeagues.Items.Add("")
        For Each item In myLeagueList
            cboLeagues.Items.Add(item)
        Next
        cboLeagues.SelectedIndex = 0
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim myCoach As coachingAppObjects.rw_coachRoot
        myCoach = coachingAppObjects.rw_coachRoot.GetCoachRoot(CType(cboExistingCoaches.SelectedItem, coachingAppObjects.ro_coachChild).CoachId)
        myCoach.CoachnameFirst = Me.txtNewCoachFirst.Text
        myCoach.CoachnameLast = Me.txtNewCoachLast.Text
        myCoach.CoachphonePrimary = Me.txtNewCoachPhonePrimary.Text
        myCoach.CoachphoneCell = Me.txtNewCoachCellPhone.Text
        myCoach.CoachEmail = Me.txtNewCoachEmail.Text

        Console.WriteLine(cboLeagues)
        myCoach.LeagueId = cboLeagues.ValueMember
        'CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId

        myCoach.Save()
        loadCoaches()
        clearCoach()
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim myCoach As coachingAppObjects.rw_coachRoot
        myCoach = coachingAppObjects.rw_coachRoot.NewCoachRoot(0)
        myCoach.CoachnameFirst = Me.txtNewCoachFirst.Text
        myCoach.CoachnameLast = Me.txtNewCoachLast.Text
        myCoach.CoachphonePrimary = Me.txtNewCoachPhonePrimary.Text
        myCoach.CoachphoneCell = Me.txtNewCoachCellPhone.Text
        myCoach.CoachEmail = Me.txtNewCoachEmail.Text
        myCoach.LeagueId = CType(cboLeagues.SelectedItem, coachingAppObjects.ro_leagueChild).LeagueId
        myCoach.Save()
        loadCoaches()
        loadLeagues()
    End Sub
    Private Sub cboExistingCoaches_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboExistingCoaches.SelectedIndexChanged
        If Not cboExistingCoaches.Text = "Please Select" Then
            Dim myCoach As coachingAppObjects.rw_coachRoot
            myCoach = coachingAppObjects.rw_coachRoot.GetCoachRoot(CType(cboExistingCoaches.SelectedItem, coachingAppObjects.ro_coachChild).CoachId)
            Me.txtNewCoachFirst.Text = myCoach.CoachnameFirst
            Me.txtNewCoachLast.Text = myCoach.CoachnameLast
            Me.txtNewCoachEmail.Text = myCoach.CoachEmail
            Me.txtNewCoachPhonePrimary.Text = myCoach.CoachphonePrimary
            Me.txtNewCoachCellPhone.Text = myCoach.CoachphoneCell


            Dim myLeague As coachingAppObjects.rw_leagueRoot
            myLeague = coachingAppObjects.rw_leagueRoot.GetleagueRoot(myCoach.LeagueId)

            cboLeagues.SelectedText = myLeague.LeagueName
            cboLeagues.ValueMember = myLeague.LeagueId

        Else
            clearCoach()
            loadLeagues()
        End If
    End Sub

    Private Sub clearCoach()
        Me.txtNewCoachFirst.Text = ""
        Me.txtNewCoachLast.Text = ""
        Me.txtNewCoachEmail.Text = ""
        Me.txtNewCoachPhonePrimary.Text = ""
        Me.txtNewCoachCellPhone.Text = ""
        Me.cboLeagues.Text = ""
    End Sub
End Class